// Spring.cpp: implementation of the Spring class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Spring.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Spring::Spring()
{
	m_nSpringSerialNum = 1;
	m_dbPreReduceValue = 0.0;
	m_dbJourney = 0.0;
	m_dbMinPress = 0.0;
	m_dbMaxPress = 0.0;
	m_dbSpringCoefficient = 0.0;
	m_nNum = 0;
}

Spring::~Spring()
{

}

BOOL Spring::IsLegal(DOUBLE dbWorkLoad, DOUBLE LoadChangeCoefficient, DOUBLE dbDisplacement)
{
	if(dbWorkLoad <= m_dbMinPress || dbWorkLoad >= m_dbMaxPress)
		return FALSE;

	DOUBLE dbSpringPosition = GetSpringDisplacement(dbWorkLoad);
	
	DOUBLE dbAnotherPosition = dbSpringPosition + dbDisplacement;

	if(dbAnotherPosition < 0 || dbAnotherPosition > m_dbJourney*m_nSpringSerialNum)
		return FALSE;

	if(GetLoadChangeCoefficient(dbDisplacement, dbWorkLoad) > LoadChangeCoefficient)
		return FALSE;

	return TRUE;
}

DOUBLE Spring::GetSpringDisplacement(DOUBLE dbWorkLoad)
{
	ASSERT(dbWorkLoad >= m_dbMinPress && dbWorkLoad <= m_dbMaxPress);

	DOUBLE dbTemp = 0.0;
	
	dbTemp = (dbWorkLoad - m_dbMinPress)/SpringCoefficient();

	return dbTemp;
}

DOUBLE Spring::GetLoadChangeCoefficient(DOUBLE dbDisplacement, DOUBLE dbWorkLoad)
{
	return fabs(dbDisplacement)*SpringCoefficient()/dbWorkLoad;
}